

package com.maoshi.shop.mp.comment.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.EntPayServiceImpl;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.maoshi.shop.mp.comment.component.WxMaInRedisConfig;
import com.maoshi.shop.mp.comment.component.WxMaServiceClusterImpl;
import com.maoshi.shop.mp.comment.config.bean.WxMiniApp;
import com.maoshi.shop.mp.comment.config.bean.WxPay;
import lombok.AllArgsConstructor;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信小程序配置文件
 * @author LGH
 */
@Configuration
@AllArgsConstructor
@ConditionalOnClass(WxMaService.class)
public class WxMaConfiguration {


    private final WxMaInRedisConfig wxMaInRedisConfig;

    private final RedissonClient redissonClient;

    private final WxMiniApp wxMiniApp;

    private final WxPay wxPay;

    @Bean
    public WxMaService wxMaService() {
        WxMaServiceClusterImpl service = new WxMaServiceClusterImpl();
        service.setWxMaConfig(wxMaInRedisConfig);
        service.setRedissonClient(redissonClient);
        return service;
    }

    @Bean
    public EntPayService entPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxMiniApp.getAppid());
        payConfig.setMchId(wxPay.getMchId());
        payConfig.setMchKey(wxPay.getMchKey());
        payConfig.setKeyPath(wxPay.getKeyPath());

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        EntPayService entPayService = new EntPayServiceImpl(wxPayService);
        return entPayService;
    }

}
